/*
 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores
 * CA 94065 USA or visit www.oracle.com if you need additional information or
 * have any questions.
 */


package com.codename1.designer;

import com.codename1.ui.util.EditableResources;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

/**
 * Allows seeking for multi-images in a directory hierarchy seamlessly
 *
 * @author Shai Almog
 */
public class FindMultiImages extends javax.swing.JDialog {
    private EditableResources res;
    public FindMultiImages(java.awt.Component c, EditableResources res) {
        this((java.awt.Frame)SwingUtilities.windowForComponent(c), true, res);
    }

    /** Creates new form FindMultiImages */
    public FindMultiImages(java.awt.Frame parent, boolean modal, EditableResources res) {
        super(parent, modal);
        this.res = res;
        initComponents();
        ModifiableJOptionPane.reverseOKCancel(ok, cancel);
        pack();
        setLocationByPlatform(true);
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        veryLow = new javax.swing.JLabel();
        selectVeryLowDensity = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        low = new javax.swing.JLabel();
        selectLowDensity = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        medium = new javax.swing.JLabel();
        selectMediumDensity = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        high = new javax.swing.JLabel();
        selectHighDensity = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        veryHigh = new javax.swing.JLabel();
        selectVeyHighDensity = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        hd = new javax.swing.JLabel();
        selectHDDensity = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        ok = new javax.swing.JButton();
        cancel = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        hd560 = new javax.swing.JLabel();
        select560Density = new javax.swing.JButton();
        jLabel13 = new javax.swing.JLabel();
        hd2 = new javax.swing.JLabel();
        select2HDDensity = new javax.swing.JButton();
        jLabel14 = new javax.swing.JLabel();
        hd4k = new javax.swing.JLabel();
        select4kDensity = new javax.swing.JButton();

        FormListener formListener = new FormListener();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Find Multi-Images");

        jLabel1.setText("Very Low Density Dir");
        jLabel1.setName("jLabel1"); // NOI18N

        veryLow.setText("[Undefined]");
        veryLow.setName("veryLow"); // NOI18N

        selectVeryLowDensity.setText("...");
        selectVeryLowDensity.setName("selectVeryLowDensity"); // NOI18N
        selectVeryLowDensity.addActionListener(formListener);

        jLabel3.setText("Low Density Dir");
        jLabel3.setName("jLabel3"); // NOI18N

        low.setText("[Undefined]");
        low.setName("low"); // NOI18N

        selectLowDensity.setText("...");
        selectLowDensity.setName("selectLowDensity"); // NOI18N
        selectLowDensity.addActionListener(formListener);

        jLabel5.setText("Medium Density Dir");
        jLabel5.setName("jLabel5"); // NOI18N

        medium.setText("[Undefined]");
        medium.setName("medium"); // NOI18N

        selectMediumDensity.setText("...");
        selectMediumDensity.setName("selectMediumDensity"); // NOI18N
        selectMediumDensity.addActionListener(formListener);

        jLabel7.setText("High Density Dir");
        jLabel7.setName("jLabel7"); // NOI18N

        high.setText("[Undefined]");
        high.setName("high"); // NOI18N

        selectHighDensity.setText("...");
        selectHighDensity.setName("selectHighDensity"); // NOI18N
        selectHighDensity.addActionListener(formListener);

        jLabel9.setText("Very High Density Dir");
        jLabel9.setName("jLabel9"); // NOI18N

        veryHigh.setText("[Undefined]");
        veryHigh.setName("veryHigh"); // NOI18N

        selectVeyHighDensity.setText("...");
        selectVeyHighDensity.setName("selectVeyHighDensity"); // NOI18N
        selectVeyHighDensity.addActionListener(formListener);

        jLabel11.setText("HD Density Dir");
        jLabel11.setName("jLabel11"); // NOI18N

        hd.setText("[Undefined]");
        hd.setName("hd"); // NOI18N

        selectHDDensity.setText("...");
        selectHDDensity.setName("selectHDDensity"); // NOI18N
        selectHDDensity.addActionListener(formListener);

        jPanel1.setName("jPanel1"); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new java.awt.GridLayout(1, 2, 5, 0));

        ok.setText("OK");
        ok.setName("ok"); // NOI18N
        ok.addActionListener(formListener);
        jPanel2.add(ok);

        cancel.setText("Cancel");
        cancel.setName("cancel"); // NOI18N
        cancel.addActionListener(formListener);
        jPanel2.add(cancel);

        jPanel1.add(jPanel2);

        jLabel12.setText("560 Density Dir");
        jLabel12.setName("jLabel12"); // NOI18N

        hd560.setText("[Undefined]");
        hd560.setName("hd560"); // NOI18N

        select560Density.setText("...");
        select560Density.setName("select560Density"); // NOI18N
        select560Density.addActionListener(formListener);

        jLabel13.setText("2HD Density Dir");
        jLabel13.setName("jLabel13"); // NOI18N

        hd2.setText("[Undefined]");
        hd2.setName("hd2"); // NOI18N

        select2HDDensity.setText("...");
        select2HDDensity.setName("select2HDDensity"); // NOI18N
        select2HDDensity.addActionListener(formListener);

        jLabel14.setText("4K Density Dir");
        jLabel14.setName("jLabel14"); // NOI18N

        hd4k.setText("[Undefined]");
        hd4k.setName("hd4k"); // NOI18N

        select4kDensity.setText("...");
        select4kDensity.setName("select4kDensity"); // NOI18N
        select4kDensity.addActionListener(formListener);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 712, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1)
                            .add(jLabel3)
                            .add(jLabel5)
                            .add(jLabel7)
                            .add(jLabel9)
                            .add(jLabel11)
                            .add(jLabel12)
                            .add(jLabel13)
                            .add(jLabel14))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(hd4k, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(select4kDensity))
                            .add(layout.createSequentialGroup()
                                .add(hd2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(select2HDDensity))
                            .add(layout.createSequentialGroup()
                                .add(hd560, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(select560Density))
                            .add(layout.createSequentialGroup()
                                .add(veryLow, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(selectVeryLowDensity))
                            .add(layout.createSequentialGroup()
                                .add(low, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 484, Short.MAX_VALUE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(selectLowDensity))
                            .add(layout.createSequentialGroup()
                                .add(medium, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 484, Short.MAX_VALUE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(selectMediumDensity))
                            .add(layout.createSequentialGroup()
                                .add(high, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 484, Short.MAX_VALUE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(selectHighDensity))
                            .add(layout.createSequentialGroup()
                                .add(veryHigh, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 484, Short.MAX_VALUE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(selectVeyHighDensity))
                            .add(layout.createSequentialGroup()
                                .add(hd, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 484, Short.MAX_VALUE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(selectHDDensity)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(veryLow)
                    .add(selectVeryLowDensity))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(low)
                    .add(selectLowDensity))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(medium)
                    .add(selectMediumDensity))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel7)
                    .add(high)
                    .add(selectHighDensity))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel9)
                    .add(veryHigh)
                    .add(selectVeyHighDensity))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel11)
                    .add(hd)
                    .add(selectHDDensity))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel12)
                    .add(hd560)
                    .add(select560Density))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel13)
                    .add(hd2)
                    .add(select2HDDensity))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel14)
                    .add(hd4k)
                    .add(select4kDensity))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == selectVeryLowDensity) {
                FindMultiImages.this.selectVeryLowDensityActionPerformed(evt);
            }
            else if (evt.getSource() == selectLowDensity) {
                FindMultiImages.this.selectLowDensityActionPerformed(evt);
            }
            else if (evt.getSource() == selectMediumDensity) {
                FindMultiImages.this.selectMediumDensityActionPerformed(evt);
            }
            else if (evt.getSource() == selectHighDensity) {
                FindMultiImages.this.selectHighDensityActionPerformed(evt);
            }
            else if (evt.getSource() == selectVeyHighDensity) {
                FindMultiImages.this.selectVeyHighDensityActionPerformed(evt);
            }
            else if (evt.getSource() == selectHDDensity) {
                FindMultiImages.this.selectHDDensityActionPerformed(evt);
            }
            else if (evt.getSource() == ok) {
                FindMultiImages.this.okActionPerformed(evt);
            }
            else if (evt.getSource() == cancel) {
                FindMultiImages.this.cancelActionPerformed(evt);
            }
            else if (evt.getSource() == select560Density) {
                FindMultiImages.this.select560DensityActionPerformed(evt);
            }
            else if (evt.getSource() == select2HDDensity) {
                FindMultiImages.this.select2HDDensityActionPerformed(evt);
            }
            else if (evt.getSource() == select4kDensity) {
                FindMultiImages.this.select4kDensityActionPerformed(evt);
            }
        }
    }// </editor-fold>//GEN-END:initComponents

    private void selectVeryLowDensityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectVeryLowDensityActionPerformed
        updateDir(veryLow);
    }//GEN-LAST:event_selectVeryLowDensityActionPerformed

    private void selectLowDensityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectLowDensityActionPerformed
        updateDir(low);
    }//GEN-LAST:event_selectLowDensityActionPerformed

    private void selectMediumDensityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectMediumDensityActionPerformed
        updateDir(medium);
    }//GEN-LAST:event_selectMediumDensityActionPerformed

    private void selectHighDensityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectHighDensityActionPerformed
        updateDir(high);
    }//GEN-LAST:event_selectHighDensityActionPerformed

    private void selectVeyHighDensityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectVeyHighDensityActionPerformed
        updateDir(veryHigh);
    }//GEN-LAST:event_selectVeyHighDensityActionPerformed

    private void selectHDDensityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectHDDensityActionPerformed
        updateDir(hd);
    }//GEN-LAST:event_selectHDDensityActionPerformed

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        dispose();
    }//GEN-LAST:event_cancelActionPerformed

    private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
        updateLabelImages(veryLow, com.codename1.ui.Display.DENSITY_VERY_LOW);
        updateLabelImages(low, com.codename1.ui.Display.DENSITY_LOW);
        updateLabelImages(medium, com.codename1.ui.Display.DENSITY_MEDIUM);
        updateLabelImages(high, com.codename1.ui.Display.DENSITY_HIGH);
        updateLabelImages(veryHigh, com.codename1.ui.Display.DENSITY_VERY_HIGH);
        updateLabelImages(hd, com.codename1.ui.Display.DENSITY_HD);
        updateLabelImages(hd560, com.codename1.ui.Display.DENSITY_560);
        updateLabelImages(hd2, com.codename1.ui.Display.DENSITY_2HD);
        updateLabelImages(hd4k, com.codename1.ui.Display.DENSITY_4K);
        dispose();
    }//GEN-LAST:event_okActionPerformed

    private void select560DensityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_select560DensityActionPerformed
        updateDir(hd560);
    }//GEN-LAST:event_select560DensityActionPerformed

    private void select2HDDensityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_select2HDDensityActionPerformed
        updateDir(hd2);
    }//GEN-LAST:event_select2HDDensityActionPerformed

    private void select4kDensityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_select4kDensityActionPerformed
        updateDir(hd4k);
    }//GEN-LAST:event_select4kDensityActionPerformed

    private void updateLabelImages(JLabel l, int dpi) {
        File dir = new File(l.getText());
        if(dir.exists() && dir.isDirectory()) {
            File[] fileList = dir.listFiles();
            for(File f : fileList) {
                try {
                    if(ImageIO.read(f) != null) {
                        com.codename1.ui.Image img = res.getImage(f.getName());
                        if(img == null) {
                            // create new multi-image
                            EditableResources.MultiImage newImage = new EditableResources.MultiImage();

                            newImage.setDpi(new int[] {dpi});
                            newImage.setInternalImages(new com.codename1.ui.EncodedImage[] {com.codename1.ui.EncodedImage.create(new FileInputStream(f))});
                            res.setMultiImage(f.getName(), newImage);
                        } else {
                            // verify this is a multi-image
                            Object o = res.getResourceObject(f.getName());
                            if(o instanceof EditableResources.MultiImage) {
                                EditableResources.MultiImage existing = (EditableResources.MultiImage)o;
                                EditableResources.MultiImage newImage = new EditableResources.MultiImage();
                                int[] dpis = new int[existing.getDpi().length + 1];
                                System.arraycopy(existing.getDpi(), 0, dpis, 0, existing.getDpi().length);
                                dpis[existing.getDpi().length] = dpi;
                                com.codename1.ui.EncodedImage[] images = new com.codename1.ui.EncodedImage[existing.getDpi().length + 1];
                                System.arraycopy(existing.getInternalImages(), 0, images, 0, existing.getDpi().length);
                                images[existing.getDpi().length] = com.codename1.ui.EncodedImage.create(new FileInputStream(f));
                                newImage.setDpi(dpis);
                                newImage.setInternalImages(images);
                                res.setMultiImage(f.getName(), newImage);
                            }
                        }
                    }
                } catch (IOException ex) {
                    // not an image file proceed
                }
            }
        }
    }

    private void updateDir(JLabel l) {
        File[] result = ResourceEditorView.showOpenFileChooserWithTitle("Select The Directory", true, "Directory");
        if(result != null && result.length == 1) {
            l.setText(result[0].getAbsolutePath());
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancel;
    private javax.swing.JLabel hd;
    private javax.swing.JLabel hd2;
    private javax.swing.JLabel hd4k;
    private javax.swing.JLabel hd560;
    private javax.swing.JLabel high;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel low;
    private javax.swing.JLabel medium;
    private javax.swing.JButton ok;
    private javax.swing.JButton select2HDDensity;
    private javax.swing.JButton select4kDensity;
    private javax.swing.JButton select560Density;
    private javax.swing.JButton selectHDDensity;
    private javax.swing.JButton selectHighDensity;
    private javax.swing.JButton selectLowDensity;
    private javax.swing.JButton selectMediumDensity;
    private javax.swing.JButton selectVeryLowDensity;
    private javax.swing.JButton selectVeyHighDensity;
    private javax.swing.JLabel veryHigh;
    private javax.swing.JLabel veryLow;
    // End of variables declaration//GEN-END:variables

}
